﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WiTeach.MaterialManagement.DAL;
using WiTeach.MaterialManagement.Model;
using System.Data;
using WiTeach.Tools;

namespace WiTeach.MaterialManagement.DAL
{
    class MaterialDAL
    {
        private static string materialTableName = "material_type_table";

        private static string materialListTableName = "ta_lesson_material";

        /// <summary>
        /// 通过课时ID 获取材料清单
        /// </summary>
        /// <param name="ClassHour_ID"></param>
        /// <returns></returns>
        public static List<Material> getMerterialListByClassID(int Lesson_ID, int ClassHour_ID)
        {
            List<Material> materialList = new List<Material>();
            string sql = "SELECT b.name,b.icon,a.lesson_id,a.material_id,a.amount FROM  ta_lesson_material a , ta_material b WHERE a.material_id = b.id AND a.lesson_id = " + Lesson_ID ;
            DataTable materialTable = SqlHelper.ExecuteDataTable(sql);
            return materialModelToList(materialTable);
        }

        /// <summary>
        /// 字典转模型
        /// </summary>
        /// <param name="dataTable"></param>
        /// <returns></returns>
        public static List<Material> materialModelToList(DataTable dataTable)
        {
            List<Material> materialList = new List<Material>();
            FileHelper fh = new FileHelper();
            if (dataTable == null)
                return null;
            foreach (DataRow dt in dataTable.Rows)
            {
                Material material = new Material();
                material.ClassHour_ID = (int)dt["lesson_id"];
                material.Lesson_ID = (int)dt["lesson_id"];
                material.MaterialList_ID = (int)dt["material_id"];
               // material.MaterialType_ID = (int)dt["MaterialType_ID"];
                //material.MaterialImage_Url = fh.GetFinderFileFullPath("\\Resource\\images\\materialImage\\", dt["MaterialImage_Url"].ToString());
                material.MaterialImage_Url = fh.GetExecutionDirectoryFileFullPath("", dt["icon"].ToString());
                material.Material_Number = " x " + dt["amount"].ToString();
                material.MaterialName = (string)dt["name"];
                materialList.Add(material);
            }
            return materialList;
        }
    }
}
